import 'package:flutter/material.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:netflix_social/theme/app_theme.dart';

class CustomRatingBar extends StatelessWidget {
  final double initialRating;
  final bool isReadOnly;
  final Function(double)? onRatingChanged;
  final double itemSize;
  final Color? activeColor;
  final Color? inactiveColor;

  const CustomRatingBar({
    Key? key,
    required this.initialRating,
    this.isReadOnly = false,
    this.onRatingChanged,
    this.itemSize = 24.0,
    this.activeColor,
    this.inactiveColor,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return RatingBar.builder(
      initialRating: initialRating,
      minRating: 1,
      direction: Axis.horizontal,
      allowHalfRating: true,
      itemCount: 5,
      itemSize: itemSize,
      ignoreGestures: isReadOnly,
      unratedColor: inactiveColor ?? Colors.grey[700],
      itemBuilder: (context, _) => Icon(
        Icons.star,
        color: activeColor ?? Colors.amber,
      ),
      onRatingUpdate: onRatingChanged ?? (rating) {},
    );
  }
}

class CustomUserRating extends StatelessWidget {
  final double rating;
  final String title;
  final String comment;
  final String userAvatar;
  final String username;
  final String timestamp;

  const CustomUserRating({
    Key? key,
    required this.rating,
    required this.title,
    required this.comment,
    required this.userAvatar,
    required this.username,
    required this.timestamp,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(bottom: 16),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: AppTheme.cardColor,
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 用户信息和评分
          Row(
            children: [
              CircleAvatar(
                radius: 20,
                backgroundImage: NetworkImage(userAvatar),
              ),
              const SizedBox(width: 12),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      username,
                      style: const TextStyle(
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: 4),
                    Row(
                      children: [
                        CustomRatingBar(
                          initialRating: rating,
                          isReadOnly: true,
                          itemSize: 16,
                        ),
                        const SizedBox(width: 8),
                        Text(
                          timestamp,
                          style: TextStyle(
                            color: Colors.grey[500],
                            fontSize: 12,
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),
          
          // 标题和评论内容
          if (title.isNotEmpty) ...[
            const SizedBox(height: 12),
            Text(
              title,
              style: const TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 16,
              ),
            ),
          ],
          
          if (comment.isNotEmpty) ...[
            const SizedBox(height: 8),
            Text(comment),
          ],
          
          // 底部操作
          const SizedBox(height: 12),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              TextButton.icon(
                icon: const Icon(
                  Icons.thumb_up_alt_outlined,
                  size: 16,
                ),
                label: const Text('有用'),
                onPressed: () {
                  // 标记为有用
                },
                style: TextButton.styleFrom(
                  foregroundColor: Colors.grey[400],
                  padding: const EdgeInsets.symmetric(
                    horizontal: 12,
                    vertical: 8,
                  ),
                ),
              ),
              TextButton.icon(
                icon: const Icon(
                  Icons.thumb_down_alt_outlined,
                  size: 16,
                ),
                label: const Text('没用'),
                onPressed: () {
                  // 标记为没用
                },
                style: TextButton.styleFrom(
                  foregroundColor: Colors.grey[400],
                  padding: const EdgeInsets.symmetric(
                    horizontal: 12,
                    vertical: 8,
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
